﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using VK.Casemeet.Lines;
using VK.Comm;
using VK.Casemeet.Areas;
using VK.Comm.TransactSql;
using System.Data;

namespace VK.Casemeet.Web.Front
{
    public partial class LineList : System.Web.UI.Page
    {
        public SearchUrlEntity url = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            url = new SearchUrlEntity();
            url.desPro = Util.GetQueryInt("desPro");
            url.staPro = Util.GetQueryInt("staPro");
            url.type = Util.GetQueryInt("type");
            string day = Util.GetQueryString("day");
            if (!string.IsNullOrEmpty(day))
            {
                url.minday = Util.GetQueryString("day").Split(new char[1] { '-' })[0];
                url.maxday = Util.GetQueryString("day").Split(new char[1] { '-' })[1];
            }
            url.mon = Util.GetQueryInt("mon");
            url.holi = Util.GetQueryInt("holi");
            url.stay = Util.GetQueryInt("stay");
            string price = Util.GetQueryString("price");
            if (!string.IsNullOrEmpty(price))
            {
                url.minprice = Util.GetQueryString("price").Split(new char[1] { '-' })[0];
                url.maxpirce = Util.GetQueryString("price").Split(new char[1] { '-' })[1];
            }
            url.grade = Util.GetQueryInt("grade");
            url.sort = Util.GetQueryInt("sort") == 0 ? 1 : Util.GetQueryInt("sort");
            url.path = Request.CurrentExecutionFilePath;

            TopLine1.AreaUrl = (this.Master as Site).GetAreaUrl();
            TopLine1.ProvinceID = (this.Master as Site).provinceID;
            TopLine1.CityID = (this.Master as Site).cityID;

            if (!IsPostBack)
            {
                aspNetPager.RecordCount = LineBLL.GetLineCount(GetCondition());
            }
        }

        protected void LoadData()
        {
            string sortField = string.Empty;
            SortMode sortMode = SortMode.Desc;
            switch (url.sort)
            {
                case 1: sortField = "Line.Created"; sortMode = SortMode.Desc; break;
            }

            rptLine.DataSource = LineBLL.GetLineList(aspNetPager.StartRecordIndex, aspNetPager.EndRecordIndex, GetCondition(), sortField, sortMode);
            rptLine.DataBind();
        }

        protected IList<Condition> GetCondition()
        {
            IList<Condition> conditionlst = new List<Condition>();
            conditionlst.Add(new Condition { TableName = "Shop", FieldName = "ProvinceID", ConditionType = ConditionType.NumeralEqual, ConditionValue = (this.Master as Site).GetCurrentAreaID(), DbType = DbType.Int32 });
            conditionlst.Add(new Condition { TableName = "Line", FieldName = "DestinationProvinceID", ConditionType = ConditionType.NumeralEqual, ConditionValue = url.desPro, DbType = DbType.Int32 });
            conditionlst.Add(new Condition { TableName = "Line", FieldName = "StagingProvinceID", ConditionType = ConditionType.NumeralEqual, ConditionValue = url.staPro, DbType = DbType.Int32 });
            conditionlst.Add(new Condition { TableName = "Line", FieldName = "Type", ConditionType = ConditionType.NumeralEqual, ConditionValue = url.type, DbType = DbType.Int32 });
            conditionlst.Add(new Condition { TableName = "Line", FieldName = "Days", ConditionType = ConditionType.NumeralMoreThanEqual, ConditionValue = string.IsNullOrEmpty(url.minday) ? "0" : url.minday, DbType = DbType.Int32 });
            conditionlst.Add(new Condition { TableName = "Line", FieldName = "Days", ConditionType = ConditionType.NumeralLittleThanEqual, ConditionValue = string.IsNullOrEmpty(url.maxday) ? "0" : url.maxday, DbType = DbType.Int32 });
            conditionlst.Add(new Condition { TableName = "Line", FieldName = "DepartureMonth", ConditionType = ConditionType.NumeralEqual, ConditionValue = url.mon, DbType = DbType.Int32 });
            conditionlst.Add(new Condition { TableName = "Line", FieldName = "Holiday", ConditionType = ConditionType.NumeralEqual, ConditionValue = url.holi, DbType = DbType.Int32 });
            conditionlst.Add(new Condition { TableName = "Line", FieldName = "Stay", ConditionType = ConditionType.NumeralEqual, ConditionValue = url.stay, DbType = DbType.Int32 });
            conditionlst.Add(new Condition { TableName = "Line", FieldName = "Price", ConditionType = ConditionType.NumeralMoreThanEqual, ConditionValue = string.IsNullOrEmpty(url.minprice) ? "0" : url.minprice, DbType = DbType.Decimal });
            conditionlst.Add(new Condition { TableName = "Line", FieldName = "Price", ConditionType = ConditionType.NumeralLittleThanEqual, ConditionValue = string.IsNullOrEmpty(url.maxpirce) ? "0" : url.maxpirce, DbType = DbType.Decimal });
            conditionlst.Add(new Condition { TableName = "Line", FieldName = "Grade", ConditionType = ConditionType.NumeralEqual, ConditionValue = url.grade, DbType = DbType.Int32 });
            return conditionlst;
        }

        protected void aspNetPager_PageChanged(object sender, EventArgs e)
        {
            LoadData();
        }

        public string PackFilterLeft()
        {
            StringBuilder html = new StringBuilder();

            #region 目的地
            html.Append("<dl><dt>目的地：</dt>");
            //无限
            if (url.desPro == 0)
            {
                html.Append("<dd><a href='").Append(url.ToUrlString()).Append(@"' class='cur'>").Append("不限").Append("</a></dd>");
            }
            else
            {
                SearchUrlEntity tempUrl = url.GetCopy() as SearchUrlEntity;
                tempUrl.desPro = 0;
                html.Append("<dd><a href='").Append(tempUrl.ToUrlString()).Append(@"'>").Append("不限").Append("</a></dd>");
            }
            //项目
            AreaEntity[] areaArray = AreaBLL.GetAreaArray(0);
            foreach (AreaEntity item in areaArray)
            {
                SearchUrlEntity tempUrl = url.GetCopy() as SearchUrlEntity;
                if (url.desPro == item.AreaID)
                {
                    html.Append("<dd><a href='").Append(tempUrl.ToUrlString()).Append(@"' class='cur'>").Append(item.Name).Append("</a></dd>");
                }
                else
                {
                    tempUrl.desPro = item.AreaID;
                    html.Append("<dd><a href='").Append(tempUrl.ToUrlString()).Append(@"'>").Append(item.Name).Append("</a></dd>");
                }
            }
            html.Append("</dl><div class='clear'></div>");
            #endregion

            #region 集结地
            html.Append("<dl><dt>集结地：</dt>");
            //无限
            if (url.staPro == 0)
            {
                html.Append("<dd><a href='").Append(url.ToUrlString()).Append(@"' class='cur'>").Append("不限").Append("</a></dd>");
            }
            else
            {
                SearchUrlEntity tempUrl = url.GetCopy() as SearchUrlEntity;
                tempUrl.staPro = 0;
                html.Append("<dd><a href='").Append(tempUrl.ToUrlString()).Append(@"'>").Append("不限").Append("</a></dd>");
            }
            //项目
            foreach (AreaEntity item in areaArray)
            {
                SearchUrlEntity tempUrl = url.GetCopy() as SearchUrlEntity;
                if (url.staPro == item.AreaID)
                {
                    html.Append("<dd><a href='").Append(tempUrl.ToUrlString()).Append(@"' class='cur'>").Append(item.Name).Append("</a></dd>");
                }
                else
                {
                    tempUrl.staPro = item.AreaID;
                    html.Append("<dd><a href='").Append(tempUrl.ToUrlString()).Append(@"'>").Append(item.Name).Append("</a></dd>");
                }
            }
            html.Append("</dl><div class='clear'></div>");
            #endregion

            return html.ToString();
        }

        public string PackFilterRight()
        {
            StringBuilder html = new StringBuilder();

            #region 活动类型
            html.Append("<div class='choicse_title'><strong>活动类型：</strong></div>")
                .Append("   <ul class='choicse_sort'>");
            //无限
            if (url.type == 0)
            {
                html.Append("<li><a href='").Append(url.ToUrlString()).Append(@"' class='cur'>").Append("不限").Append("</a></li>");
            }
            else
            {
                SearchUrlEntity tempUrl = url.GetCopy() as SearchUrlEntity;
                tempUrl.type = 0;
                html.Append("<li><a href='").Append(tempUrl.ToUrlString()).Append(@"'>").Append("不限").Append("</a></li>");
            }
            //项目
            foreach (ListItem item in LineHelper.TypeData)
            {
                SearchUrlEntity tempUrl = url.GetCopy() as SearchUrlEntity;
                if (url.type == int.Parse(item.Value))
                {
                    html.Append("<li><a href='").Append(tempUrl.ToUrlString()).Append(@"' class='cur'>").Append(item.Text).Append("</a></li>");
                }
                else
                {
                    tempUrl.type = int.Parse(item.Value);
                    html.Append("<li><a href='").Append(tempUrl.ToUrlString()).Append(@"'>").Append(item.Text).Append("</a></li>");
                }
            }
            html.Append("</ul><div class='clear'></div>");
            #endregion

            #region 活动时长
            html.Append("<div class='choicse_title'><strong>活动时长：</strong></div>")
                .Append("   <ul class='choicse_sort'>");
            //无限
            if (string.IsNullOrEmpty(url.minday) && string.IsNullOrEmpty(url.maxday))
            {
                html.Append("<li><a href='").Append(url.ToUrlString()).Append(@"' class='cur'>").Append("不限").Append("</a></li>");
            }
            else
            {
                SearchUrlEntity tempUrl = url.GetCopy() as SearchUrlEntity;
                tempUrl.minday = string.Empty; tempUrl.maxday = string.Empty;
                html.Append("<li><a href='").Append(tempUrl.ToUrlString()).Append(@"'>").Append("不限").Append("</a></li>");
            }
            //项目
            foreach (ListItem item in LineHelper.DayData)
            {
                SearchUrlEntity tempUrl = url.GetCopy() as SearchUrlEntity;
                if ((url.minday + "-" + url.maxday) == item.Value)
                {
                    html.Append("<li><a href='").Append(tempUrl.ToUrlString()).Append(@"' class='cur'>").Append(item.Text).Append("</a></li>");
                }
                else
                {
                    tempUrl.minday = item.Value.Split(new char[1] { '-' })[0]; 
                    tempUrl.maxday = item.Value.Split(new char[1] { '-' })[1];
                    html.Append("<li><a href='").Append(tempUrl.ToUrlString()).Append(@"'>").Append(item.Text).Append("</a></li>");
                }
            }
            html.Append("</ul><div class='clear'></div>");
            #endregion

            #region 出行月份
            html.Append("<div class='choicse_title'><strong>出行月份：</strong></div>")
                .Append("   <ul class='choicse_sort'>");
            //无限
            if (url.mon == 0)
            {
                html.Append("<li><a href='").Append(url.ToUrlString()).Append(@"' class='cur'>").Append("不限").Append("</a></li>");
            }
            else
            {
                SearchUrlEntity tempUrl = url.GetCopy() as SearchUrlEntity;
                tempUrl.mon = 0;
                html.Append("<li><a href='").Append(tempUrl.ToUrlString()).Append(@"'>").Append("不限").Append("</a></li>");
            }
            //项目
            foreach (ListItem item in LineHelper.MonData)
            {
                SearchUrlEntity tempUrl = url.GetCopy() as SearchUrlEntity;
                if (url.mon == int.Parse(item.Value))
                {
                    html.Append("<li><a href='").Append(tempUrl.ToUrlString()).Append(@"' class='cur'>").Append(item.Text).Append("</a></li>");
                }
                else
                {
                    tempUrl.mon = int.Parse(item.Value);
                    html.Append("<li><a href='").Append(tempUrl.ToUrlString()).Append(@"'>").Append(item.Text).Append("</a></li>");
                }
            }
            html.Append("</ul><div class='clear'></div>");
            #endregion

            #region 特别节日
            html.Append("<div class='choicse_title'><strong>特别节日：</strong></div>")
                .Append("   <ul class='choicse_sort'>");
            //无限
            if (url.holi == 0)
            {
                html.Append("<li><a href='").Append(url.ToUrlString()).Append(@"' class='cur'>").Append("不限").Append("</a></li>");
            }
            else
            {
                SearchUrlEntity tempUrl = url.GetCopy() as SearchUrlEntity;
                tempUrl.holi = 0;
                html.Append("<li><a href='").Append(tempUrl.ToUrlString()).Append(@"'>").Append("不限").Append("</a></li>");
            }
            //项目
            foreach (ListItem item in LineHelper.HolidayData)
            {
                SearchUrlEntity tempUrl = url.GetCopy() as SearchUrlEntity;
                if (url.holi == int.Parse(item.Value))
                {
                    html.Append("<li><a href='").Append(tempUrl.ToUrlString()).Append(@"' class='cur'>").Append(item.Text).Append("</a></li>");
                }
                else
                {
                    tempUrl.holi = int.Parse(item.Value);
                    html.Append("<li><a href='").Append(tempUrl.ToUrlString()).Append(@"'>").Append(item.Text).Append("</a></li>");
                }
            }
            html.Append("</ul><div class='clear'></div>");
            #endregion

            #region 住宿方式
            html.Append("<div class='choicse_title'><strong>住宿方式：</strong></div>")
                .Append("   <ul class='choicse_sort'>");
            //无限
            if (url.stay == 0)
            {
                html.Append("<li><a href='").Append(url.ToUrlString()).Append(@"' class='cur'>").Append("不限").Append("</a></li>");
            }
            else
            {
                SearchUrlEntity tempUrl = url.GetCopy() as SearchUrlEntity;
                tempUrl.stay = 0;
                html.Append("<li><a href='").Append(tempUrl.ToUrlString()).Append(@"'>").Append("不限").Append("</a></li>");
            }
            //项目
            foreach (ListItem item in LineHelper.StayData)
            {
                SearchUrlEntity tempUrl = url.GetCopy() as SearchUrlEntity;
                if (url.stay == int.Parse(item.Value))
                {
                    html.Append("<li><a href='").Append(tempUrl.ToUrlString()).Append(@"' class='cur'>").Append(item.Text).Append("</a></li>");
                }
                else
                {
                    tempUrl.stay = int.Parse(item.Value);
                    html.Append("<li><a href='").Append(tempUrl.ToUrlString()).Append(@"'>").Append(item.Text).Append("</a></li>");
                }
            }
            html.Append("</ul><div class='clear'></div>");
            #endregion

            #region 活动预算
            html.Append("<div class='choicse_title'><strong>活动预算：</strong></div>")
                .Append("   <ul class='choicse_sort'>");
            //无限
            if (string.IsNullOrEmpty(url.minprice) && string.IsNullOrEmpty(url.maxpirce))
            {
                html.Append("<li><a href='").Append(url.ToUrlString()).Append(@"' class='cur'>").Append("不限").Append("</a></li>");
            }
            else
            {
                SearchUrlEntity tempUrl = url.GetCopy() as SearchUrlEntity;
                tempUrl.minprice = string.Empty; tempUrl.maxpirce = string.Empty;
                html.Append("<li><a href='").Append(tempUrl.ToUrlString()).Append(@"'>").Append("不限").Append("</a></li>");
            }
            //项目
            foreach (ListItem item in LineHelper.PriceData)
            {
                SearchUrlEntity tempUrl = url.GetCopy() as SearchUrlEntity;
                if ((url.minprice + "-" + url.maxpirce) == item.Value)
                {
                    html.Append("<li><a href='").Append(tempUrl.ToUrlString()).Append(@"' class='cur'>").Append(item.Text).Append("</a></li>");
                }
                else
                {
                    tempUrl.minprice = item.Value.Split(new char[1] { '-' })[0];
                    tempUrl.maxpirce = item.Value.Split(new char[1] { '-' })[1];
                    html.Append("<li><a href='").Append(tempUrl.ToUrlString()).Append(@"'>").Append(item.Text).Append("</a></li>");
                }
            }
            html.Append("</ul><div class='clear'></div>");
            #endregion

            #region 活动分级
            html.Append("<div class='choicse_title'><strong>活动分级：</strong></div>")
                .Append("   <ul class='choicse_sort'>");
            //无限
            if (url.grade == 0)
            {
                html.Append("<li><a href='").Append(url.ToUrlString()).Append(@"' class='cur'>").Append("不限").Append("</a></li>");
            }
            else
            {
                SearchUrlEntity tempUrl = url.GetCopy() as SearchUrlEntity;
                tempUrl.grade = 0;
                html.Append("<li><a href='").Append(tempUrl.ToUrlString()).Append(@"'>").Append("不限").Append("</a></li>");
            }
            //项目
            foreach (ListItem item in LineHelper.GradeData)
            {
                SearchUrlEntity tempUrl = url.GetCopy() as SearchUrlEntity;
                if (url.grade == int.Parse(item.Value))
                {
                    html.Append("<li><a href='").Append(tempUrl.ToUrlString()).Append(@"' class='cur'>").Append(item.Text).Append("</a></li>");
                }
                else
                {
                    tempUrl.grade = int.Parse(item.Value);
                    html.Append("<li><a href='").Append(tempUrl.ToUrlString()).Append(@"'>").Append(item.Text).Append("</a></li>");
                }
            }
            html.Append("</ul><div class='clear'></div>");
            #endregion

            return html.ToString();
        }
    }
}